using System;
using System.Drawing;
using System.ComponentModel;

namespace WbsEditor
{
    [Browsable(true)]
    public class WbsSettings : IDisposable
    {
        public WbsSettings()
        {
            Layout = WbsSettings.LayoutSize.Normal;
            HorizontalPadding = 10;
            VerticalPadding = 10;
            IndentationVerticalLayout = 20;
            VerticalPaddingAtRoot = 30;
            ShadowOffset = 4;
            FontFamilyName = "Arial";
        }

        public void Dispose()
        {
            DisposeFonts();
        }

        public enum LayoutSize
        {
            Normal = 0,
            Small = 1,
            Large = 2
        }

        private LayoutSize _layoutSize = LayoutSize.Normal;
        public LayoutSize Layout
        {
            get
            {
                return _layoutSize;
            }
            set
            {
                _layoutSize = value;
                DisposeFonts();
            }
        }

        private void DisposeFonts()
        {
            if (_smallFont != null)
            {
                _smallFont.Dispose();
                _smallFont = null;
                _normalFont.Dispose();
                _normalFont = null;
            }
        }

        [Browsable(false)]
        public Size DefaultNodeSize
        {
            get
            {
                switch (Layout)
                {
                    case WbsSettings.LayoutSize.Normal:
                        return new Size(150, 70);
                    case WbsSettings.LayoutSize.Small:
                        return new Size(80, 40);
                    case WbsSettings.LayoutSize.Large:
                        return new Size(200, 100);
                }
                return new Size(150, 70);
            }
        }

        private Font _normalFont;
        [Browsable(false)]
        public Font NormalFont
        {
            get
            {
                if (null == _normalFont)
                {
                    if (Layout == WbsSettings.LayoutSize.Normal
                        || Layout == WbsSettings.LayoutSize.Large)
                        _normalFont = new Font(FontFamilyName, 11f);
                    else
                        _normalFont = new Font(FontFamilyName, 8f);
                }
                return _normalFont;
            }
        }
        private Font _smallFont;
        [Browsable(false)]
        public Font SmallFont
        {
            get
            {
                if (null == _smallFont)
                {
                    if (Layout == WbsSettings.LayoutSize.Normal
                        || Layout == WbsSettings.LayoutSize.Large)
                        _smallFont = new Font(FontFamilyName, 9f);
                    else
                        _smallFont = new Font(FontFamilyName, 6f);
                }
                return _smallFont;
            }
        }
        private int _horizontalPadding;
        public int HorizontalPadding { get { return _horizontalPadding; } set { _horizontalPadding = value; } }
        private int _verticalPadding;
        public int VerticalPadding { get { return _verticalPadding; } set { _verticalPadding = value; } }
        private int _verticalPaddingAtRoot;
        public int VerticalPaddingAtRoot { get { return _verticalPaddingAtRoot; } set { _verticalPaddingAtRoot = value; } }
        private int _indentationVerticalLayout;
        public int IndentationVerticalLayout { get { return _indentationVerticalLayout; } set { _indentationVerticalLayout = value; } }
        private int _zoom = 100;
        [Browsable(false)]
        public int Zoom
        {
            get { return _zoom; }
            set
            {
                _zoom = value;
                if (_zoom > 200)
                    _zoom = 200;
                else if (_zoom < 10)
                    _zoom = 10;
            }
        }
        private string _highlightName;
        [Browsable(false)]
        public string HighlightName { get { return _highlightName; } set { _highlightName = value; } }

        private string _findString;
        [Browsable(false)]
        public string FindString { get { return _findString; } set { _findString = value; } }

        private int _shadowOffset;
        public int ShadowOffset { get { return _shadowOffset; } set { _shadowOffset = value; } }

        private bool _checkPlausis;
        public bool CheckPlausis { get { return _checkPlausis; } set { _checkPlausis = value; } }

        private bool _printPlausis;
        public bool PrintPlausis { get { return _printPlausis; } set { _printPlausis = value; } }

        private Color _progressBarColor;
        public Color ProgressBarColor { get { return _progressBarColor; } set { _progressBarColor = value; } }

        private Color _innerProgressColor;
        public Color InnerProgressColor { get { return _innerProgressColor; } set { _innerProgressColor = value; } }

        private Color _titleColor;
        public Color TitleColor { get { return _titleColor; } set { _titleColor = value; } }

        private Color _plausiMarkColor;
        public Color PlausiMarkColor { get { return _plausiMarkColor; } set { _plausiMarkColor = value; } }

        private Color _findResultColor;
        public Color FindResultColor { get { return _findResultColor; } set { _findResultColor = value; } }

        private string _fontFamilyName;
        public string FontFamilyName 
        { 
            get 
            { 
                return _fontFamilyName; 
            } 
            
            set 
            {
                if (_fontFamilyName != value)
                {
                    _fontFamilyName = value;
                    DisposeFonts();
                }
            }
        }

        private bool _useWbsCodes = true;
        public bool UseWbsCodes { get { return _useWbsCodes; } set { _useWbsCodes = value; } }

        public bool HighlightCurrentIteration
        {
            get;
            set;
        }

        public WbsSettings Clone()
        {
            WbsSettings clone = new WbsSettings();

            clone.CheckPlausis = CheckPlausis;
            clone.FontFamilyName = FontFamilyName;
            clone.HighlightName = HighlightName;
            clone.HorizontalPadding = HorizontalPadding;
            clone.IndentationVerticalLayout = IndentationVerticalLayout;
            clone.InnerProgressColor = InnerProgressColor;
            clone.Layout = Layout;
            clone.PlausiMarkColor = PlausiMarkColor;
            clone.PrintPlausis = PrintPlausis;
            clone.ProgressBarColor = ProgressBarColor;
            clone.ShadowOffset = ShadowOffset;
            clone.TitleColor = TitleColor;
            clone.UseWbsCodes = UseWbsCodes;
            clone.VerticalPadding = VerticalPadding;
            clone.VerticalPaddingAtRoot = VerticalPaddingAtRoot;
            clone.Zoom = Zoom;
            clone.FindResultColor = FindResultColor;
            clone.HighlightCurrentIteration = HighlightCurrentIteration;

            return clone;
        }
    }
}